package com.icss.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.icss.beans.NewsVO;
import com.icss.daos.NewsDao;

/**
 * @author 廖鹏
 * @version 创建时间：2010-3-29 上午09:59:07
 * 类说明
 */
@SuppressWarnings("serial")
public class NewsServlet extends HttpServlet {

	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String action=request.getParameter("action");
		
		if("add".equals(action)){
			publishNewsAction(request,response);
		}
		if("list".equals(action)){
			newsListAction(request,response);
		}
	}
	
	private void publishNewsAction(HttpServletRequest request,HttpServletResponse response)
			throws ServletException,IOException {
		
		PrintWriter out = response.getWriter();
		
		String newsTitle = request.getParameter("newsTitle");
		
		String publishTime = request.getParameter("publishTime");
		
		String newsContent = request.getParameter("newsContent");
		
		System.out.println(publishTime);
		
		NewsVO newsVO = new NewsVO();
		
		newsVO.setNewsTitle(newsTitle);
		newsVO.setNewsContent(newsContent);
		
		NewsDao newsDao = new NewsDao();
		
		boolean b = newsDao.addNew(newsVO);
		
		if(b){
			out.println("ok");
		}
		
		
	}
	
	@SuppressWarnings("unchecked")
	private void newsListAction(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		int currentPage=1,pageSize=5;
		String currentPageS=request.getParameter("Page");
		String pageSizeS=request.getParameter("pageSize");
		if(null!=currentPageS){
			currentPage=Integer.parseInt(currentPageS);
		}
		if(null!=pageSizeS){
			pageSize=Integer.parseInt(currentPageS);
		}
		NewsDao newsDao = new NewsDao();
		
		List list = newsDao.getPageNews(currentPage, pageSize, "no");
		
		List<NewsVO> newsList = (List<NewsVO>)list.get(0);
		
		int totaPage = (Integer)list.get(1);
		int totalRecord = (Integer)list.get(2);
		request.setAttribute("newsList", newsList);
		request.setAttribute("totalRecord", totalRecord);
		request.setAttribute("totalPage", totaPage);
		
		request.getRequestDispatcher("/page/news/newsList.jsp").forward(request, response);

	}

}
